package com.yycx.module.bus.provider.handler.process.form;

import com.yycx.bpm.provider.handler.TaskCompleteHandler;
import com.yycx.bpm.provider.model.TaskRequest;
import com.yycx.common.security.OpenUser;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.module.bus.client.entity.BusCustom;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.BusOrderDetails;
import com.yycx.module.bus.provider.service.BusCustomService;
import com.yycx.module.bus.provider.service.BusDeptService;
import com.yycx.module.bus.provider.service.BusOrderDetailsService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 获取任务表单数据
 *
 * @author zyf
 */
@Component
public class HuikuanFormTaskDataHandler implements TaskCompleteHandler {


    @Autowired
    private BusCustomService customService;
    @Autowired
    private BusDeptService deptService;
    @Autowired
    private BusOrderDetailsService orderDetailsService;


    @Override
    public Map<String, Object> initTaskData(ProcessInstance processInstance, Task task, Map<String, Object> variables) {
        Map<String, Object> ret = new HashMap<>(5);
        String businessKey = processInstance.getBusinessKey();
        BusOrderDetails customReturn = orderDetailsService.getById(businessKey);
        if (FlymeUtils.isNotEmpty(customReturn)) {
            Long deptId = customReturn.getDeptId();
            BusDept dept = deptService.getById(deptId);
            Long customId = customReturn.getCustomId();
            BusCustom custom = customService.getById(customId);
            ret.put("customName", custom.getCustomName());
            ret.put("deptName", dept.getDeptName());
            ret.put("returnAmount", customReturn.getReturnAmount());

        }
        return ret;
    }

    /**
     * 表单提交后业务处理
     *
     * @param taskRequest
     * @param processInstance
     * @param task
     * @param completeVariables
     */
    @Override
    public void completeAfter(TaskRequest taskRequest, ProcessInstance processInstance, Task task, Map<String, Object> completeVariables, OpenUser user) {

    }

}
